﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GenericTools;
using ReportingManagement.Models;

namespace ReportingManagement.Components
{
    /// <summary>
    /// Interaction logic for ReportMergingEditor.xaml
    /// </summary>
    public partial class ReportMergingEditor : Window
    {
        public ReportMergingEditor()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string name = mergeEditor.MergingName;
            string description = mergeEditor.MergingDescription;
            var sourceReports = mergeEditor.SourceReportIds;
            int destinationReportId = mergeEditor.DestinationReportId;

            List<string> indicatorDefinitions = null;
            try
            {
                indicatorDefinitions = mergeEditor.IndicatorDefinitions;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            try
            {
                bool saved = Utilities.SaveReportMergingDefinition(name, description,
                    destinationReportId, sourceReports, indicatorDefinitions);
                if (!saved)
                {
                    MessageBox.Show("Data was not saved!", AppConfiguration.ApplicationName,
                        MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    this.DialogResult = true;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, AppConfiguration.ApplicationName,
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }    
}
